﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Response.Write(Request.Files[0].FileName);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            if (uploadFile.PostedFile != null)
            {
                // File was sent
                var postedFile = uploadFile.PostedFile;
                int filesize = postedFile.ContentLength;
                string extension = Path.GetExtension(postedFile.FileName);
                if (filesize == 0)
                {
                    Label1.Text = "请上传文件！";
                }
                else
                {
                    /*byte[] header = new byte[4];
                    postedFile.InputStream.Read(header, 0, 4);
                    if (header[0] != 0xD4 | header[1] != 0xC3 | header[2] != 0xB2 | header[3] != 0xA1) //check magic
                        Label1.Text = "不是pcap或cap文件";*/
                    int maxlength = Convert.ToInt32(ConfigurationManager.AppSettings["maxUploadFileLength"])*1024;
                    if (!extension.ToLower().Equals(".cap") && !extension.ToLower().Equals(".pcap"))
                        Label1.Text = "不是pcap或cap文件";
                    else if (filesize > maxlength)//默认最大100M
                    {
                        Label1.Text = "文件太大";
                    }
                    else
                    {
                        string filepath = Path.Combine(Server.MapPath("~/UploadFiles/"), postedFile.FileName);
                        if (!File.Exists(filepath))
                            postedFile.SaveAs(filepath); //save the upload file 
                        string md5 = Utils.GetHash(filepath);
                        Label1.Text = "md5值为：" + md5;
                        if(ConfigurationManager.AppSettings["forceRecomputed"].ToLower() == "yes") //删除旧的,重新计算
                            Utils.DeleteFileData(md5);
                        if (!Utils.IsFileExisted(md5)) //如果文件不存在就解析pacp文件就插入数据库
                        {
                           Utils.ParseFile(filepath, postedFile.FileName, filesize, md5);
                        }
                        int fileid = Utils.GetFileId(md5); //get fileid from the database
                        if (fileid > 0)
                        {
                            string url = "Charts.aspx?fileid=" + fileid.ToString();
                            Response.Redirect(url);
                        }
                    }
                       
                }
            }
            else
            {
                Label1.Text = "请上传文件！";
            }

        }
    }
}